package com.xiaoyao.myframeapp.utils.Glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.xiaoyao.myframeapp.utils.SpUtils;

/**
 * @Author xiaoyao
 * @Email xiao80206040@qq.com
 * @Explain Glide 照片墙
 * @Created by Administrator on 2017/3/31 0031 08:57
 */
public class PhotoWallTransform extends BitmapTransformation {
    //TODO Glide 照片墙
    private static int spanCount = 3;
    public static final int PHOTOWALLTRANSFORM_RATIO = 1;//TODO 按照比例
    public static final int PHOTOWALLTRANSFORM_WIDTH = 2;//TODO 按照宽度
    private static int RatioOrWidth = PHOTOWALLTRANSFORM_RATIO;

    public PhotoWallTransform(Context context) {
        super(context);
    }


    public PhotoWallTransform(Context context, int spanCount, int RatioOrWidth) {
        super(context);
        this.spanCount = spanCount;
        this.RatioOrWidth = RatioOrWidth;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap bitmap = null;
        switch (RatioOrWidth) {
            case PHOTOWALLTRANSFORM_RATIO:
                bitmap = roundCrop(pool, toTransform);
                break;
            case PHOTOWALLTRANSFORM_WIDTH:
                bitmap = roundCrop_w(pool, toTransform);
                break;
        }
        return bitmap;
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap resource) {

        int imageWidth = Integer.valueOf(SpUtils.get("width", 0).toString()) / spanCount;
//        L.e("h=" + resource.getHeight());
//        L.e("w=" + resource.getWidth());
//        int height = resource.getHeight();
        int weight = resource.getWidth();
        float ratio = weight / imageWidth;

        Matrix matrix = new Matrix();
//        matrix.postScale(0.8f, 0.8f); //长和宽放大缩小的比例
        matrix.postScale(ratio, ratio); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), matrix, true);
        return resizeBmp;

    }

    //TODO 宽
    private static Bitmap roundCrop_w(BitmapPool pool, Bitmap resource) {

//        L.e("h=" + resource.getHeight());
//        L.e("w=" + resource.getWidth());
//        int height = resource.getHeight();
        int weight = resource.getWidth();

        float ratio = (float) spanCount / weight;

        Matrix matrix = new Matrix();
//        matrix.postScale(0.8f, 0.8f); //长和宽放大缩小的比例
        matrix.postScale(ratio, ratio); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), matrix, true);
        return resizeBmp;

    }

    @Override
    public String getId() {
//        return getClass().getName() + Math.round(radius);

        return getClass().getSimpleName();
    }
}